﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Runtime.InteropServices;
using System.Windows.Forms;
using System.Xml;
using Assist;
using Autodesk.Revit.DB;
using Autodesk.Revit.UI;
using YJKRevitWndr.InsertDoorAndWindow.Cmd;
using YJKRevitWndr.InsertDoorAndWindow.InsertOperation;
using YArchitech.Controls;
using YArchitech.LIB;
using YArchitech.LIB.YJKControls;
using YArchitech.Revit;
using YJKArchUtils.Utils;
using YJKRevitBase.WinForm.HookSuit;
using YJKRevitPreview;

namespace YJKRevitWndr.InsertDoorAndWindow.Forms
{
	public partial class InsertDoorAndWinForm : System.Windows.Forms.Form
	{
		public bool AddTag
		{
			get
			{
				return this.checkBox1.Checked;
			}
		}

		public OperationType OperaType
		{
			get
			{
				return this.m_operationType;
			}
			set
			{
				this.m_operationType = value;
			}
		}

		public int Tab
		{
			get
			{
				return this.tctlFamilyPosition.SelectedIndex;
			}
		}

		[DllImport("user32.dll")]
		public static extern void keybd_event(byte bVk, byte bScan, int dwFlags, int dwExtraInfo);

		[DllImport("user32.dll", EntryPoint = "SendMessageA")]
		public static extern int SendMessage(IntPtr hwnd, int wMsg, IntPtr wParam, IntPtr lParam);

		private void InitControlValue()
		{
			InitComboxData initComboxData = new InitComboxData();
			List<string> list = new List<string>();
			List<string> list2 = new List<string>();
			if ((int)this.m_builtInCategory == -2000023)
			{
				list = initComboxData.DoorHigh;
				list2 = initComboxData.DoorBottomHigh;
			}
			else
			{
				list = initComboxData.WindowHigh;
				list2 = initComboxData.WindowBottomHigh;
			}
			List<string> doorAndWinWidth = initComboxData.DoorAndWinWidth;
			this.cmbDoorAndWinWidth.Items.Clear();
			foreach (string item in doorAndWinWidth)
			{
				this.cmbDoorAndWinWidth.Items.Add(item);
			}
			this.cmbDoorAndWinWidth.SelectedIndex = 0;
			this.cmbDoorAndWinHigh.Items.Clear();
			foreach (string item2 in list)
			{
				this.cmbDoorAndWinHigh.Items.Add(item2);
			}
			this.cmbDoorAndWinHigh.SelectedIndex = 0;
			List<string> fireProtectionGrade = initComboxData.FireProtectionGrade;
			this.cmbFireProtectionGrade.Items.Clear();
			foreach (string item3 in fireProtectionGrade)
			{
				this.cmbFireProtectionGrade.Items.Add(item3);
			}
			this.cmbFireProtectionGrade.SelectedIndex = 0;
			this.cmbDoorAndWinBottomHigh.Items.Clear();
			foreach (string item4 in list2)
			{
				this.cmbDoorAndWinBottomHigh.Items.Add(item4);
			}
			if ((int)this.m_builtInCategory == -2000023)
			{
				this.cmbDoorAndWinBottomHigh.Text = "0";
				return;
			}
			this.cmbDoorAndWinBottomHigh.Text = "900";
		}

		private void InitFormControlName()
		{
			if ((int)this.m_builtInCategory == -2000023)
			{
				this.Text = "插入门";
				return;
			}
			this.Text = "插入窗";
			this.lblDoorAndWinWidth.Text = "窗宽(mm)：";
			this.lblDoorAndWinHigh.Text = "窗高(mm)：";
			this.lblDoorAndWinBottomHigh.Text = "窗台高(mm)：";
		}

		private void ChangeFormSize(TabControl tabControl)
		{
			if (!(tabControl.SelectedTab.Text == "族库"))
			{
				base.Size = new Size((int)(368.0 * this.m_dXScale), (int)(449.0 * this.m_dYScale));
				if (this.m_strSelectSymbolDoc != null)
				{
					this.m_symbol = this.m_strSelectSymbolDoc;
				}
				this.InitFamilyListByDoc();
				return;
			}
			base.Size = new Size((int)(190.0 * this.m_dXScale), (int)(449.0 * this.m_dYScale));
			if (this.m_SelectSymbolLib != null)
			{
				this.m_symbol = this.m_SelectSymbolLib;
				this.SetControlData();
				this.FireProtectionGrade(this.m_symbol.Family.Name, this.m_symbol.Name);
				return;
			}
			this.m_choiceFamilyForm.LoadImageDefault();
			this.picLib3D.Image = this.m_choiceFamilyForm.Image3D;
			this.picLib2D.Image = this.m_choiceFamilyForm.Image2D;
			this.m_strLoadFamily = this.m_choiceFamilyForm.m_strFamilyName;
			this.DelLoadFamily = new LoadFamily(this.LoadFam);
			this.cmbFireProtectionGrade.Enabled = false;
		}

		private void InitFamilyListByDoc()
		{
			try
			{
				this.InitTreeView();
				if (this.m_strSelectFamilyName != null && this.m_strSelectFamilyName != "")
				{
					this.GetLastNode();
				}
				if (this.m_tnSelect == null)
				{
					this.m_tnSelect = this.tvwFamilyListByDoc.Nodes[0].Nodes[0].Nodes[0];
				}
				this.tvwFamilyListByDoc.SelectedNode = this.m_tnSelect;
			}
			catch (Exception)
			{
			}
		}

		private void InitTreeView()
		{
			string text = "";
			if ((int)this.m_builtInCategory == -2000023)
			{
				text = "门";
			}
			else if ((int)this.m_builtInCategory == -2000014)
			{
				text = "窗";
			}
			this.tvwFamilyListByDoc.Nodes.Clear();
			TreeNode treeNode = new TreeNode();
			treeNode.Text = text;
			List<Family> list = new List<Family>();
			foreach (Element element in new FilteredElementCollector(this.m_uiApp.ActiveUIDocument.Document).OfClass(typeof(FamilySymbol)).OfCategory(this.m_builtInCategory).ToElements())
			{
				FamilySymbol familySymbol = element as FamilySymbol;
				if (!list.Contains(familySymbol.Family, new FamilyIdComparer()))
				{
					list.Add(familySymbol.Family);
				}
			}
			foreach (Family family in list)
			{
				TreeNode treeNode2 = new TreeNode();
				treeNode2.Text = family.Name;
				treeNode2.Tag = family;
				foreach (FamilySymbol familySymbol2 in (from ff in family.GetSymbols()
				orderby ff.Name
				select ff).ToList<FamilySymbol>())
				{
					TreeNode treeNode3 = new TreeNode();
					treeNode3.Text = familySymbol2.Name;
					treeNode3.Tag = familySymbol2;
					treeNode2.Nodes.Add(treeNode3);
				}
				treeNode.Nodes.Add(treeNode2);
			}
			this.tvwFamilyListByDoc.Nodes.Add(treeNode);
			this.tvwFamilyListByDoc.ExpandAll();
		}

		public void RefshForm(string str1, string str2)
		{
			this.m_strSelectFamilyName = str1;
			this.m_strSelectFamilySymbolName = str2;
			this.InitTreeView();
		}

		public void GetDoorData()
		{
			this.m_doorData.Symbol = this.m_symbol;
			this.m_doorData.DoorAndWinWidth = this.cmbDoorAndWinWidth.Text;
			this.m_doorData.DoorAndWinHigh = this.cmbDoorAndWinHigh.Text;
			this.m_doorData.FireProtectionGrade = this.cmbFireProtectionGrade.Text;
			this.m_doorData.DoorAndWinBottomHigh = this.cmbDoorAndWinBottomHigh.Text;
		}

		private bool ValidationOfLegality()
		{
			List<string> list = new List<string>();
			string text = this.cmbDoorAndWinWidth.Text;
			string text2 = this.cmbDoorAndWinHigh.Text;
			string text3 = this.cmbDoorAndWinBottomHigh.Text;
			bool flag = false;
			string text4 = this.lblDoorAndWinWidth.Text;
			text4 = text4.Substring(0, 2);
			if (this.IsNum(text))
			{
				if (!Geometry.GreaterThan(double.Parse(text), 0.0))
				{
					list.Add("【" + text4 + "】数值错误，请输入大于0的整数");
				}
			}
			else
			{
				list.Add("【" + text4 + "】数值错误，请输入大于0的整数");
			}
			string text5 = this.lblDoorAndWinHigh.Text;
			text5 = text5.Substring(0, 2);
			if (this.IsNum(text2))
			{
				if (!Geometry.GreaterThan(double.Parse(text2), 0.0))
				{
					list.Add("【" + text5 + "】数值错误，请输入大于0的整数");
				}
			}
			else
			{
				list.Add("【" + text5 + "】数值错误，请输入大于0的整数");
			}
			string text6 = this.lblDoorAndWinBottomHigh.Text;
			text6 = text6.Substring(0, 3);
			if (this.IsNum(text3))
			{
				if (Geometry.LessThan(double.Parse(text3), 0.0))
				{
					list.Add("【" + text6 + "】数值错误，请输入大于等于0的整数");
					flag = true;
				}
			}
			else
			{
				list.Add("【" + text6 + "】数值错误，请输入大于等于0的整数");
				flag = true;
			}
			string message = string.Empty;
			if (list.Count == 0)
			{
				return true;
			}
			if (list.Count == 1)
			{
				message = list[0];
			}
			else if (list.Count == 2)
			{
				message = list[0] + "\r\n" + list[1];
			}
			else if (list.Count == 3)
			{
				message = string.Concat(new string[]
				{
					list[0],
					"\r\n",
					list[1],
					"\r\n",
					list[2]
				});
			}
			YJKMessageBox.Show(message);
			string text7 = list[0];
			if (text7.Contains(text4))
			{
				this.cmbDoorAndWinWidth.Focus();
			}
			else if (text7.Contains(text5))
			{
				this.cmbDoorAndWinHigh.Focus();
			}
			else
			{
				this.cmbDoorAndWinBottomHigh.Focus();
			}
			if (flag)
			{
				if ((int)this.m_builtInCategory == -2000023)
				{
					this.cmbDoorAndWinBottomHigh.Text = this.m_strBottomHigh;
				}
				else
				{
					this.cmbDoorAndWinBottomHigh.Text = this.m_strWinHigh;
				}
			}
			return false;
		}

		private bool IsNum(string str)
		{
			bool result;
			try
			{
				Convert.ToInt32(str);
				result = true;
			}
			catch (Exception)
			{
				result = false;
			}
			return result;
		}

		private bool ReadUserData()
		{
			try
			{
				this.m_xmlDoc = new XmlDocument();
				if (!File.Exists(this.m_DefaultValueFilePath))
				{
					this.tctlFamilyPosition.SelectedIndex = 0;
					this.ChangeFormSize(this.tctlFamilyPosition);
					return false;
				}
				this.m_xmlDoc.Load(this.m_DefaultValueFilePath);
				XmlNode xmlNode = this.m_xmlDoc.SelectSingleNode("root");
				if (xmlNode == null)
				{
					MessageBox.Show("文件格式不符！");
					return false;
				}
				XmlNode xmlNode2 = xmlNode.SelectSingleNode("DialogData");
				if (xmlNode2 == null)
				{
					MessageBox.Show("文件中缺少数据！");
					return false;
				}
				this.ReadFamilyData(xmlNode2);
			}
			catch (Exception)
			{
			}
			return true;
		}

		private void ReadFamilyData(XmlNode xnNode)
		{
			XmlNode xmlNode = xnNode.SelectSingleNode("类型名称");
			XmlNode xmlNode2 = xmlNode.SelectSingleNode("族名称");
			this.m_strSelectFamilyName = xmlNode2.InnerText;
			xmlNode2 = xmlNode.SelectSingleNode("防火等级");
			this.cmbFireProtectionGrade.Text = xmlNode2.InnerText;
			XmlNode xmlNode3 = xnNode.SelectSingleNode("族数据");
			XmlNode xmlNode4 = xmlNode3.SelectSingleNode("族位置");
			string innerText = xmlNode4.InnerText;
			xmlNode4 = xmlNode3.SelectSingleNode("族类型名称");
			this.m_strSelectFamilySymbolName = xmlNode4.InnerText;
			if (innerText == "0")
			{
				xmlNode4 = xmlNode3.SelectSingleNode("文件名");
				this.m_strFamilyFileName = xmlNode4.InnerText;
				xmlNode4 = xmlNode3.SelectSingleNode("族2D路径");
				string innerText2 = xmlNode4.InnerText;
				this.m_Image2DPath = innerText2;
				xmlNode4 = xmlNode3.SelectSingleNode("族3D路径");
				string innerText3 = xmlNode4.InnerText;
				this.m_Image3DPath = innerText3;
				this.m_strLoadFamily = this.m_strSelectFamilyName;
				this.LoadFam();
				FamilySymbol symbol = null;
				if (this.IsExistence(this.m_strSelectFamilySymbolName, out symbol))
				{
					this.m_symbol = symbol;
				}
				this.FireProtectionGrade(this.m_strSelectFamilyName, this.m_strSelectFamilySymbolName);
				this.SetControlData();
				this.SetImage(innerText2, innerText3);
			}
			this.tctlFamilyPosition.SelectedIndex = int.Parse(innerText);
			if (this.m_tnSelect == null)
			{
				this.tctlFamilyPosition.SelectedIndex = 0;
				return;
			}
			xmlNode2 = xmlNode.SelectSingleNode("宽");
			this.cmbDoorAndWinWidth.Text = xmlNode2.InnerText;
			xmlNode2 = xmlNode.SelectSingleNode("高");
			this.cmbDoorAndWinHigh.Text = xmlNode2.InnerText;
			xmlNode4 = xmlNode3.SelectSingleNode("底高");
			this.cmbDoorAndWinBottomHigh.Text = xmlNode4.InnerText;
		}

		private void GetLastNode()
		{
			foreach (object obj in this.tvwFamilyListByDoc.Nodes)
			{
				foreach (object obj2 in ((TreeNode)obj).Nodes)
				{
					TreeNode treeNode = (TreeNode)obj2;
					if (treeNode.Text == this.m_strSelectFamilyName)
					{
						this.GetLastNode(treeNode);
						return;
					}
				}
			}
		}

		private void GetLastNode(TreeNode tn)
		{
			foreach (object obj in tn.Nodes)
			{
				TreeNode treeNode = (TreeNode)obj;
				if (treeNode.Text == this.m_strSelectFamilySymbolName)
				{
					this.m_tnSelect = treeNode;
				}
			}
		}

		private void SetImage(string str2DPath, string str3DPath)
		{
			Image image = Image.FromFile(str3DPath, true);
			Image image2 = Image.FromFile(str2DPath, true);
			this.picLib2D.Image = image2;
			this.picLib3D.Image = image;
		}

		private void WriteUserData(string fileName)
		{
			try
			{
				this.m_xmlDoc = new XmlDocument();
				bool bExistXml;
				if (File.Exists(fileName))
				{
					this.m_xmlDoc.Load(fileName);
					bExistXml = true;
				}
				else
				{
					XmlNode newChild = this.m_xmlDoc.CreateXmlDeclaration("1.0", "gb2312", null);
					this.m_xmlDoc.AppendChild(newChild);
					XmlElement newChild2 = this.m_xmlDoc.CreateElement("root");
					this.m_xmlDoc.AppendChild(newChild2);
					bExistXml = false;
				}
				this.AddDataToXML("DialogData", bExistXml);
				this.m_xmlDoc.Save(fileName);
			}
			catch
			{
			}
		}

		private void AddDataToXML(string dataNodeName, bool bExistXml)
		{
			XmlNode xmlNode = this.m_xmlDoc.SelectSingleNode("root");
			if (bExistXml)
			{
				XmlNode xmlNode2 = xmlNode.SelectSingleNode(dataNodeName);
				if (xmlNode2 != null)
				{
					xmlNode2.RemoveAll();
					xmlNode.RemoveChild(xmlNode2);
				}
			}
			XmlElement newChild = this.m_xmlDoc.CreateElement(dataNodeName);
			xmlNode.AppendChild(newChild);
			XmlNode xmlNode3 = xmlNode.SelectSingleNode(dataNodeName);
			this.AddFamilyData(ref newChild, ref xmlNode3);
			this.AddTypeNameData(ref newChild, ref xmlNode3);
		}

		private void AddFamilyData(ref XmlElement xmlElem, ref XmlNode subNode)
		{
			string text = "族数据";
			xmlElem = this.m_xmlDoc.CreateElement(text);
			subNode.AppendChild(xmlElem);
			XmlNode xmlNode = subNode.SelectSingleNode(text);
			string text2 = this.tctlFamilyPosition.SelectedIndex.ToString();
			Dictionary<string, string> dictionary = new Dictionary<string, string>();
			dictionary.Add("族位置", text2);
			if (int.Parse(this.cmbDoorAndWinBottomHigh.Text) < 0)
			{
				string value = "0";
				dictionary.Add("底高", value);
			}
			else
			{
				dictionary.Add("底高", this.cmbDoorAndWinBottomHigh.Text);
			}
			dictionary.Add("族类型名称", this.m_symbol.Name);
			if (text2 == "0")
			{
				if (this.m_choiceFamilyForm.Image2DPath == "" || this.m_choiceFamilyForm.Image2DPath == null)
				{
					this.m_choiceFamilyForm.Image2DPath = this.m_Image2DPath;
				}
				dictionary.Add("族2D路径", this.m_choiceFamilyForm.Image2DPath);
				if (this.m_choiceFamilyForm.Image3DPath == "" || this.m_choiceFamilyForm.Image3DPath == null)
				{
					this.m_choiceFamilyForm.Image3DPath = this.m_Image3DPath;
				}
				dictionary.Add("族3D路径", this.m_choiceFamilyForm.Image3DPath);
				dictionary.Add("文件名", this.m_strFamilyFileName);
			}
			this.SetDataToXML(dictionary, ref xmlNode);
		}

		private void AddTypeNameData(ref XmlElement xmlElem, ref XmlNode subNode)
		{
			string text = "类型名称";
			xmlElem = this.m_xmlDoc.CreateElement(text);
			subNode.AppendChild(xmlElem);
			XmlNode xmlNode = subNode.SelectSingleNode(text);
			this.SetDataToXML(new Dictionary<string, string>
			{
				{
					"无对应项",
					""
				},
				{
					"族名称",
					this.m_symbol.Family.Name
				},
				{
					"高",
					this.cmbDoorAndWinHigh.Text
				},
				{
					"宽",
					this.cmbDoorAndWinWidth.Text
				},
				{
					"防火等级",
					this.cmbFireProtectionGrade.Text
				}
			}, ref xmlNode);
		}

		private void SetDataToXML(Dictionary<string, string> m_dicXml, ref XmlNode xnUser)
		{
			foreach (KeyValuePair<string, string> keyValuePair in m_dicXml)
			{
				XmlNode xmlNode = this.m_xmlDoc.CreateElement(keyValuePair.Key);
				xmlNode.InnerText = keyValuePair.Value;
				xnUser.AppendChild(xmlNode);
			}
		}

		private void Get2DImageDoc(TreeNode node)
		{
			FamilySymbol familySymbol = null;
			if (node.Level == 1)
			{
				familySymbol = (FamilySymbol)node.FirstNode.Tag;
			}
			else if (node.Level == 2)
			{
				familySymbol = (FamilySymbol)node.Tag;
			}
			Document doc = this.m_uiApp.ActiveUIDocument.Document.EditFamily(familySymbol.Family);
			this.Refresh2DImgCurrentDoc(doc, familySymbol);
		}

		private void Refresh2DImgCurrentDoc(Autodesk.Revit.DB.Document doc, FamilySymbol fs)
		{
			this.legend = LegendCommon.GetFamily2DImg(this.m_uiApp, doc, LegendType.DoorWindows);
			if (this.legend != null)
			{
				this.IsChange = true;
				this.m_previewer.Enabled = true;
				this.picDoc2D.Enabled = false;
				this.m_previewer.BringToFront();
				this.m_previewer.Refresh();
				return;
			}
			this.m_previewer.Enabled = false;
			this.picDoc2D.Enabled = true;
			this.picDoc2D.BringToFront();
			this.picDoc2D.Image = fs.GetPreviewImage(new Size(this.m_imgW, this.m_imgH));
		}

		private void previewer_Paint(object sender, PaintEventArgs e)
		{
			Graphics graphics = e.Graphics;
			graphics.Clear(System.Drawing.Color.White);
			this.RefreshPreview(graphics);
		}

		private void RefreshPreview(Graphics g)
		{
			try
			{
				if (this.IsChange)
				{
					this.IsChange = false;
					this.m_previewer.Curves = this.legend.Curves;
					this.m_previewer.ZoomExtents();
				}
			}
			catch (Exception)
			{
			}
		}

		public void FireProtectionGrade(string strFamilyName, string strSymbolName)
		{
			if (strFamilyName.Contains("防火门") || strSymbolName.Contains("FM") || strSymbolName.Contains("FC") || strSymbolName.Contains("防火窗"))
			{
				this.cmbFireProtectionGrade.Enabled = true;
				return;
			}
			if (strFamilyName.Contains("防火卷帘") || strSymbolName.Contains("WFJ") || strSymbolName.Contains("TFJ") || strSymbolName.Contains("GFJ"))
			{
				this.cmbFireProtectionGrade.Enabled = true;
				return;
			}
			this.cmbFireProtectionGrade.Enabled = false;
			this.cmbFireProtectionGrade.Text = "";
		}

		private Family LoadFam()
		{
			Family family = null;
			Transaction transaction = new Transaction(this.m_doc);
			try
			{
				string text = Path.Combine(this.m_strFullPath, this.m_strLoadFamily + ".rfa");
				transaction.Start("load families");
				if (!this.IsExistence(this.m_strLoadFamily, out family))
				{
					this.m_doc.LoadFamily(text, out family);
				}
				this.m_symbol = family.GetSymbols().FirstOrDefault<FamilySymbol>();
				transaction.Commit();
			}
			catch (Exception)
			{
				transaction.RollBack();
			}
			return family;
		}

		private bool IsExistence(string strFamilyName, out Family family)
		{
			FilteredElementCollector filteredElementCollector = new FilteredElementCollector(this.m_doc);
			filteredElementCollector.OfClass(typeof(Family));
			if (filteredElementCollector != null)
			{
				foreach (Element element in filteredElementCollector.ToElements())
				{
					if (((Family)element).Name == strFamilyName)
					{
						family = (Family)element;
						return true;
					}
				}
			}
			family = null;
			return false;
		}

		private bool IsExistence(string strFamilyName, out FamilySymbol symbol)
		{
			FilteredElementCollector filteredElementCollector = new FilteredElementCollector(this.m_doc);
			filteredElementCollector.OfClass(typeof(FamilySymbol));
			if (filteredElementCollector != null)
			{
				foreach (Element element in filteredElementCollector.ToElements())
				{
					FamilySymbol familySymbol = element as FamilySymbol;
					if (((FamilySymbol)element).Name == strFamilyName && familySymbol.Family.Name == this.m_symbol.Family.Name)
					{
						symbol = (FamilySymbol)element;
						return true;
					}
				}
			}
			symbol = null;
			return false;
		}

		public void SetControlData()
		{
			Parameter parameter = this.m_symbol.get_Parameter(BuiltInParameter.CASEWORK_WIDTH);
			this.cmbDoorAndWinWidth.Text = parameter.AsValueString();
			Parameter parameter2 = this.m_symbol.get_Parameter(BuiltInParameter.FAMILY_HEIGHT_PARAM);
			this.cmbDoorAndWinHigh.Text = parameter2.AsValueString();
			Parameter parameter3 = this.m_symbol.get_Parameter(BuiltInParameter.DOOR_FIRE_RATING);
			if (parameter3 != null)
			{
				this.cmbFireProtectionGrade.Text = parameter3.AsString();
			}
			Parameter parameter4 = this.m_symbol.GetParameter("防火等级");
			if (parameter4 != null)
			{
				this.cmbFireProtectionGrade.Text = parameter4.AsString();
			}
		}

		protected override void DefWndProc(ref Message m)
		{
			int msg = m.Msg;
			if (msg != 1125)
			{
				base.DefWndProc(ref m);
				return;
			}
			if (this.ValidationOfLegality())
			{
				this.GetDoorData();
				this.OperaType = OperationType.Refresh;
				this.SendEsc();
				return;
			}
			this.SetControlData();
		}

		private void SendEsc()
		{
			this.SendMessages();
			int bVk = 27;
			int dwFlags = 0;
			int dwFlags2 = 2;
			InsertDoorAndWinForm.keybd_event((byte)bVk, 0, dwFlags, 0);
			InsertDoorAndWinForm.keybd_event((byte)bVk, 0, dwFlags2, 0);
		}

		private void SendMessages()
		{
			int wMsg = 7;
			if ((int)this.m_builtInCategory == -2000023)
			{
				InsertDoorAndWinForm.SendMessage(CmdInsertDoor._hWndRevit.Handle, wMsg, IntPtr.Zero, IntPtr.Zero);
				return;
			}
			InsertDoorAndWinForm.SendMessage(CmdInsertWin._hWndRevit.Handle, wMsg, IntPtr.Zero, IntPtr.Zero);
		}

		private new void Enter()
		{
			int bVk = 13;
			int dwFlags = 0;
			int dwFlags2 = 2;
			InsertDoorAndWinForm.keybd_event((byte)bVk, 0, dwFlags, 0);
			InsertDoorAndWinForm.keybd_event((byte)bVk, 0, dwFlags2, 0);
		}

		private void WriteLocation()
		{
			FileStream fileStream = new FileStream(Product.UserDataLocation + "\\" + "InsertDoorAndWin.dat", FileMode.Create);
			StreamWriter streamWriter = new StreamWriter(fileStream);
			streamWriter.WriteLine(base.Location.X);
			streamWriter.WriteLine(base.Location.Y);
			streamWriter.Flush();
			streamWriter.Close();
			fileStream.Close();
		}

		private void ReadLocation()
		{
			try
			{
				string path = Product.UserDataLocation + "\\" + "InsertDoorAndWin.dat";
				if (!File.Exists(path))
				{
					base.StartPosition = FormStartPosition.CenterScreen;
				}
				else
				{
					base.StartPosition = FormStartPosition.Manual;
					FileStream fileStream = new FileStream(path, FileMode.Open);
					StreamReader streamReader = new StreamReader(fileStream);
					streamReader.BaseStream.Seek(0L, SeekOrigin.Begin);
					int x = Convert.ToInt32(streamReader.ReadLine());
					int y = Convert.ToInt32(streamReader.ReadLine());
					base.Location = new System.Drawing.Point(x, y);
					streamReader.Close();
					fileStream.Close();
				}
			}
			catch (Exception)
			{
			}
		}

		public void SelectLastNode()
		{
			this.tvwFamilyListByDoc.SelectedNode = this.m_LastNode;
		}

		public void SelectNewNode()
		{
			this.GetLastNode();
			this.tvwFamilyListByDoc.SelectedNode = this.m_tnSelect;
		}

		public InsertDoorAndWinForm(ExternalCommandData cmdData, BuiltInCategory category, string strFamily, string strFamilyType)
		{
			this.InitializeComponent();
			this.m_strSelectFamilyName = strFamily;
			this.m_strSelectFamilySymbolName = strFamilyType;
			this.m_previewer = new Previewer(1u);
			this.m_previewer.Paint += this.previewer_Paint;
			this.tabPage2.Controls.Add(this.m_previewer);
			this.m_previewer.Location = new System.Drawing.Point(this.picDoc2D.Left, this.picDoc2D.Top);
			this.m_previewer.Size = new Size(this.picDoc2D.Width, this.picDoc2D.Height);
			this.m_previewer.TabStop = false;
			this.m_imgW = this.picDoc2D.Width;
			this.m_imgH = this.picDoc2D.Height;
			this.m_cmdData = cmdData;
			this.m_uiApp = cmdData.Application;
			this.m_doc = this.m_uiApp.ActiveUIDocument.Document;
			this.m_builtInCategory = category;
			this.m_insertMode = InsertMode.eByPickPoint;
			string appRootPath = AssistFunc.GetAppRootPath();
			string versionNumber = cmdData.Application.Application.VersionNumber;
			this.m_strFullPath = appRootPath + "\\FamilyLib\\Family\\";
			this.m_strFullPath += versionNumber;
			if ((int)this.m_builtInCategory == -2000023)
			{
				this.m_DefaultValueFilePath = Product.InstallLocation + "\\Data\\插入门配置文件.xml";
				this.m_strFullPath += "\\Door\\";
			}
			else
			{
				this.m_DefaultValueFilePath = Product.InstallLocation + "\\Data\\插入窗配置文件.xml";
				this.m_strFullPath += "\\Window\\";
			}
			WinDPIManager.GetWinFormXYDpi(ref this.m_dXScale, ref this.m_dYScale);
			int width = (int)((double)this.tvwFamilyListByDoc.Width * this.m_dXScale);
			int height = (int)((double)this.tvwFamilyListByDoc.Height * this.m_dYScale);
			this.tvwFamilyListByDoc.Size = new Size(width, height);
		}

		private void InsertDoorForm_Load(object sender, EventArgs e)
		{
			//base.Icon = Resource.YArchitechIcon;
			this.ReadLocation();
			this.checkBox1.Checked = true;
			this.m_color = System.Drawing.Color.CornflowerBlue;
			this.InitFormControlName();
			this.m_MouseHook = new MouseHook(this);
			this.m_MouseHook.HookStart();
			this.InitControlValue();
			this.m_choiceFamilyForm = new ChoiceFamilyForm(this.m_cmdData, this.m_builtInCategory);
			if (!this.m_strSelectFamilyName.IsEmpty<char>())
			{
				this.tctlFamilyPosition.SelectedIndex = 1;
				CmdInsertDoor.m_strFamilyName = "";
				CmdInsertWin.m_strFamilyName = "";
			}
			else
			{
				this.ReadUserData();
			}
			this.GetDoorData();
		}

		private void tctlFamilyPosition_SelectedIndexChanged(object sender, EventArgs e)
		{
			TabControl tabControl = sender as TabControl;
			this.ChangeFormSize(tabControl);
		}

		private void InsertDoorForm_FormClosing(object sender, FormClosingEventArgs e)
		{
			this.WriteUserData(this.m_DefaultValueFilePath);
			this.WriteLocation();
			this.m_MouseHook.HookStop();
			this.SendEsc();
		}

		private void InsertDoorForm_KeyDown(object sender, KeyEventArgs e)
		{
			Keys keyCode = e.KeyCode;
			if (keyCode != Keys.Return)
			{
				if (keyCode == Keys.Escape)
				{
					base.Close();
					return;
				}
			}
			else
			{
				this.SendEsc();
			}
		}

		private void tvwFamilyListByDoc_AfterSelect(object sender, TreeViewEventArgs e)
		{
			this.m_tnSelect = this.tvwFamilyListByDoc.SelectedNode;
			this.m_tnSelect.BackColor = this.m_color;
			if (this.m_tnSelect.Level == 0)
			{
				return;
			}
			if (this.m_tnSelect.Level == 2)
			{
				this.m_strSelectSymbolDoc = (this.m_tnSelect.Tag as FamilySymbol);
			}
			else if (this.m_tnSelect.Level == 1)
			{
				this.m_strSelectSymbolDoc = (FamilySymbol)this.m_tnSelect.FirstNode.Tag;
			}
			this.DelRefreshImage2D = new RefreshImage2D(this.Get2DImageDoc);
			string name = this.m_strSelectSymbolDoc.Family.Name;
			string name2 = this.m_strSelectSymbolDoc.Name;
			this.m_strSelectFamilyName = name;
			this.m_strSelectFamilySymbolName = name2;
			this.FireProtectionGrade(name, name2);
			this.m_symbol = this.m_strSelectSymbolDoc;
			this.SetControlData();
			this.GetDoorData();
			this.OperaType = OperationType.Refresh;
			this.Enter();
		}

		private void pictureBox1_Click(object sender, EventArgs e)
		{
			if (this.m_choiceFamilyForm.ShowDialog() == DialogResult.Cancel && this.m_choiceFamilyForm.Image3D != null && this.m_choiceFamilyForm.Image2D != null)
			{
				this.picLib3D.Image = this.m_choiceFamilyForm.Image3D;
				this.picLib2D.Image = this.m_choiceFamilyForm.Image2D;
				this.m_strLoadFamily = this.m_choiceFamilyForm.m_strFamilyName;
				this.m_strFamilyFileName = this.m_choiceFamilyForm.m_strImageName;
				this.FireProtectionGrade(this.m_strFamilyFileName, this.m_strLoadFamily);
			}
			this.OperaType = OperationType.LoadFamily;
			this.DelLoadFamily = new LoadFamily(this.LoadFam);
			this.SendEsc();
		}

		private void tvwFamilyListByDoc_BeforeSelect(object sender, TreeViewCancelEventArgs e)
		{
			System.Drawing.Color backColor = this.tvwFamilyListByDoc.BackColor;
			if (this.m_tnSelect == null)
			{
				return;
			}
			this.m_tnSelect.BackColor = backColor;
			this.m_LastNode = this.m_tnSelect;
		}

		private void cmbDoorAndWinBottomHigh_Click(object sender, EventArgs e)
		{
			this.m_strBottomHigh = this.cmbDoorAndWinBottomHigh.Text;
			this.m_strWinHigh = this.cmbDoorAndWinBottomHigh.Text;
		}

		private void btnPickPoint_Click(object sender, EventArgs e)
		{
			this.btnPickPoint.Checked = true;
			this.btnButtress.Checked = false;
			this.btnEqualWall.Checked = false;
			this.OperaType = OperationType.SwitchOperationMode;
			this.SendEsc();
			this.m_insertMode = InsertMode.eByPickPoint;
		}

		private void btnButtress_Click(object sender, EventArgs e)
		{
			this.btnPickPoint.Checked = false;
			this.btnButtress.Checked = true;
			this.btnEqualWall.Checked = false;
			this.OperaType = OperationType.SwitchOperationMode;
			this.SendEsc();
			this.m_insertMode = InsertMode.eByButtress;
		}

		private void btnEqualWall_Click(object sender, EventArgs e)
		{
			this.btnPickPoint.Checked = false;
			this.btnButtress.Checked = false;
			this.btnEqualWall.Checked = true;
			this.OperaType = OperationType.SwitchOperationMode;
			this.SendEsc();
			this.m_insertMode = InsertMode.eByEqualWall;
		}

		private void btnPickPoint_MouseEnter(object sender, EventArgs e)
		{
			base.Focus();
		}

		private void btnButtress_MouseEnter(object sender, EventArgs e)
		{
			base.Focus();
		}

		private void btnEqualWall_MouseEnter(object sender, EventArgs e)
		{
			base.Focus();
		}

		private UIApplication m_uiApp;

		private Document m_doc;

		public DoorData m_doorData = new DoorData();

		public InsertMode m_insertMode;

		private BuiltInCategory m_builtInCategory;

		public FamilySymbol m_symbol;

		private MouseHook m_MouseHook;

		private string m_DefaultValueFilePath;

		private XmlDocument m_xmlDoc;

		public RefreshImage2D DelRefreshImage2D;

		public LoadFamily DelLoadFamily;

		public TreeNode m_tnSelect;

		private Previewer m_previewer;

		private Legend legend;

		private int m_imgW;

		private int m_imgH;

		public bool m_bFirst;

		private ChoiceFamilyForm m_choiceFamilyForm;

		private System.Drawing.Color m_color;

		private string m_Image2DPath;

		private string m_Image3DPath;

		private string m_strSelectFamilyName;

		private string m_strSelectFamilySymbolName;

		private string m_strFamilyFileName;

		private string m_strLoadFamily;

		public string m_strPlieWidth;

		public string m_strEqualWall;

		private OperationType m_operationType;

		public FamilySymbol m_SelectSymbolLib;

		private bool IsChange = true;

		private ExternalCommandData m_cmdData;

		private string m_strFullPath;

		private double m_dXScale = 1.0;

		private double m_dYScale = 1.0;

		private FamilySymbol m_strSelectSymbolDoc;

		public SelectNode DelSelectNode;

		private TreeNode m_LastNode;

		private string m_strBottomHigh = "0";

		private string m_strWinHigh = "900";
	}
}
